<style>
  .header-font {
    font-size: 15px;
    font-weight: bold;
  }
  .line {
    margin: 10px 0;
    width: 100%;
    border: 0.5px solid rgb(218, 218, 218);
  }
  .table {
    margin-top: 10px;
  }
  .paging {
    float:right;
    margin-top:10px;
  }
</style>
<template>
  <div>
    <div class='header-font'>
      <Icon type="md-apps" />
      <span>banner设置</span>
    </div>
    <div class="line"></div>
    <Button
      icon="md-add"
      type="primary"
      @click="handleClick">
      添加banner
    </Button>
    <sys-banner-drawer
      @sys-banner-valid="handleEdit">
    </sys-banner-drawer>
    <delete-modal
      @delete-valid="handleConfirmDelete">
    </delete-modal>
    <Table
      class="table"
      :columns="columns"
      :data="historyData"
      :loading="loading"
      border>
    </Table>
    <table-page
      class="paging"
      :dataCount="dataCount"
      @change-page="changePage"
      @change-page-size="changePageSize"
      :pageSize="pageSize">
    </table-page>
  </div>
</template>
<script>
import {
  mapActions,
  mapState
} from 'vuex'
import SysBannerDrawer from '_c/sys-banner-drawer'
import DeleteModal from '_c/delete-modal'
import TablePage from '_c/table-page'
export default {
  components: {
    SysBannerDrawer,
    DeleteModal,
    TablePage
  },
  data () {
    return {
      page: 1,
      title: '系统管理banner设置',
      id: null,
      rows: 20,
      loading: true,
      confirmData: {},
      columns: [
        {
          title: '序号',
          type: 'index2',
          width: 80,
          align: 'center',
          render: (h, params) => {
            return h('span', params.index + (this.page - 1) * this.pageSize + 1)
          }
        },
        {
          title: '图片',
          key: 'img',
          minWidth: 200,
          align: 'center',
          render: (h, params) => {
            if (params.row.url) {
              return h('div', [
                h('img', {
                  attrs: {
                    src: params.row.host + params.row.url,
                    style: 'width: 170px; height: 90px;'
                  },
                  style: {
                    marginTop: '5px'
                  }
                })
              ])
            } else {
              return h('span', '-')
            }
          }
        },
        {
          title: '标题',
          key: 'title',
          minWidth: 220,
          align: 'center',
          render: (h, params) => {
            return h('span', params.row.title ? params.row.title : '-')
          }
        },
        {
          title: '起始时间',
          key: 'time',
          minWidth: 180,
          align: 'center',
          render: (h, params) => {
            return h(
              'span',
              {},
              `${this.$moment(params.row.begintime).format('YYYY/MM/DD')} ~ ${this.$moment(params.row.endtime).format('YYYY/MM/DD')}`
            )
          }
        },
        {
          title: '排序',
          key: 'sort',
          minWidth: 120,
          align: 'center'
        },
        {
          title: '状态',
          key: 'state',
          align: 'center',
          minWidth: 120,
          render: (h, params) => {
            return h(
              'span',
              {},
              params.row.state === 0 ? '启动' : params.row.state === 1 ? '关闭' : '-'
            )
          }
        },
        {
          title: '操作',
          key: 'cz',
          minWidth: 196,
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h('Button', {
                props: {
                  type: 'primary',
                  size: 'small',
                  ghost: 'ghost'
                },
                style: {
                  marginRight: '5px'
                },
                on: {
                  click: () => {
                    this.handleClick(params.row.id, params.row)
                  }
                }
              }, '编辑'),
              h('Button', {
                props: {
                  type: 'error',
                  size: 'small',
                  ghost: 'ghost'
                },
                style: {
                  marginRight: '5px'
                },
                on: {
                  click: () => {
                    this.handleDelete(params.row.id)
                  }
                }
              }, '删除')
            ])
          }
        }
      ]
    }
  },
  computed: {
    ...mapState({
      sysBannerList: state => state.sysBanner.sysBannerList,
      dataCount: state => state.sysBanner.sysBannerDataCount,
      historyData: state => state.sysBanner.sysBannerHistoryData,
      pageSize: state => state.sysBanner.sysBannerPageSize,
      row: state => state.sysBanner.row
    })
  },
  methods: {
    ...mapActions([
      'getSysBannerList',
      'getSysBannerCurrentRow',
      'editSysBannerData',
      'deleteSysBannerData',
      'changeSysBannerPage',
      'changeSysBannerPageSize',
      'updateDeleteModalState',
      'updateModalState'
    ]),
    handleClick (id, row) {
      let newRow = {}
      // this.id = id
      if (row) {
        newRow = Object.assign({}, row)
      }
      this.getSysBannerCurrentRow(newRow)
      // console.log(this.row)
      this.updateModalState(true)
    },
    handleDelete (id) {
      // console.log(id)
      this.id = id
      this.updateDeleteModalState(true)
    },
    async handleConfirmDelete () {
      this.loading = true
      // console.log(this.id)
      const resp = await this.deleteSysBannerData({ id: this.id })
      if (resp.data.state === '000') {
        this.$Message.error('删除成功')
        this.getSysBannerList({
          page: this.page,
          rows: this.rows
        }).then((res) => {
          if (res.data.state === '000') {
            this.loading = false
          }
        })
      } else {
        this.$Message.error('删除失败')
      }
      this.updateDeleteModalState(false)
      this.changePage(this.page)
    },
    async handleEdit (playload) {
      // console.log(playload)
      if (!playload.url) {
        this.$Message.warning('请选择banner图片！')
        return false
      }
      this.loading = true
      const resp = await this.editSysBannerData(playload)
      if (resp.data.state === '000') {
        this.$Message.success('编辑成功')
        this.getSysBannerList({
          page: this.page,
          rows: this.rows
        }).then((res) => {
          if (res.data.state === '000') {
            this.loading = false
          }
        })
      } else {
        this.$Message.error(resp.data.msg)
      }
      this.updateModalState(false)
      // this.updateConfirmModalState(true)
    },
    changePage (index) {
      this.loading = true
      this.page = index
      this.changeSysBannerPage(index)
      this.getSysBannerList({
        page: index,
        rows: this.rows
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    },
    changePageSize (value) {
      this.loading = true
      this.rows = value
      this.changeSysBannerPageSize(value)
      this.changPage(this.page)
      this.getSysBannerList({
        rows: value
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    }
  },
  created () {
    this.getSysBannerList({}).then((res) => {
      if (res.data.state === '000') {
        this.loading = false
      }
    })
  }
}
</script>
